export default {
  namespaced: true,
  state: {
    // 侧边导航路由菜单
    asideNavs: [
      {
        label: '仪表盘',
        path: '/dashBoard',
        icon: 'el-icon-s-data',
        roles: '*'
      },
      {
        label: '商品管理',
        icon: 'el-icon-s-goods',
        roles: ['a', 'b', 'c', 'admin'],
        children: [
          {
            label: '商品分类管理',
            path: '/cateLists',
            roles: ['a', 'b', 'c', 'admin']
          },
          {
            label: '商品管理',
            path: '/itemLists',
            roles: ['a', 'b', 'c', 'admin']
          }
        ]
      },
      {
        label: '个人中心',
        path: '/user',
        icon: 'el-icon-user-solid',
        roles: ['a', 'b', 'c', 'admin']
      },
      {
        label: '设置',
        path: '/settings',
        icon: 'el-icon-s-tools',
        roles: ['a', 'b', 'c']
      }
    ]
  },
  getters: {
    // 当前角色可以访问的 侧边导航
    authNavs (state) {
      // 获取当前用户的角色
      const role = localStorage.getItem('role')
      // 定义空数组
      const authNavs = []
      state.asideNavs.forEach(nav => {
        // 遍历第一层
        // 当前导航的 roles中包含 用户role 或者 roles === '*'
        if (nav.roles === '*' || nav.roles.includes(role)) {
          const _nav = { ...nav }
          // 判断是否有children 子导航进行处理
          // 创建新对象 赋值 当前这个nav
          const _children = []
          if (nav.children) {
            // 保存 可以访问子 nav 菜单
            nav.children.forEach(subNav => {
              if (subNav.roles === '*' || subNav.roles.includes(role)) {
                _children.push(subNav)
              }
            })
            _nav.children = _children
          }
          authNavs.push(_nav)
        }
      })
      return authNavs
    }
  }
}
